<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <span>微信公众号：</span>
            <el-select size="medium" v-trim v-model="pageForm.wxid" placeholder="请选择" @change="change">
                <el-option
                        v-for="item in wxConfigs"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
        </div>

        <div class="btn-group tool-button mt5">
            <el-input placeholder="请输入内容" v-trim v-model="pageForm.searchKeyword" @keyup.enter.native="doSearch">
                <el-select v-trim v-model="pageForm.searchName" slot="prepend" placeholder="查询类型" style="width: 120px;">
                    <el-option label="昵称" value="nickname"></el-option>
                    <el-option label="内容" value="content"></el-option>
                </el-select>
                <el-button slot="append" icon="el-icon-search" @click="doSearch"></el-button>
            </el-input>
        </div>

    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                @sort-change='pageOrder'
                size="small"
                header-align="center"
                style="width: 100%"
                :default-sort="{prop: 'opAt', order: 'descending'}"
        >

            <el-table-column type="expand">
                <template slot-scope="scope">
                    <el-form label-position="left" inline class="el-table-expand" size="mini">
                        <el-form-item label="回复内容">
                            <span v-if="scope.row.replyId && scope.row.replyId!=''">{{ scope.row.reply.content }}</span>
                        </el-form-item>
                        <el-form-item label="回复时间">
                            <span v-if="scope.row.replyId && scope.row.replyId!=''">
                                {{ formatAt(scope.row.reply.opAt) }}</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>

            <el-table-column label="昵称" header-align="left" prop="nickname"
                             :show-overflow-tooltip="true" width="180">
                <template slot-scope="scope">
                    <img v-if="scope.row.headimgurl!=''" :src="scope.row.headimgurl" width="30" height="30">
                    <span>{{scope.row.nickname}}</span>
                </template>
            </el-table-column>

            <el-table-column label="消息类型" header-align="left" prop="type" width="80">

            </el-table-column>

            <el-table-column label="消息内容" header-align="left" prop="content"
                             :show-overflow-tooltip="true">
            </el-table-column>


            <el-table-column label="是否回复" header-align="left" prop="replyId"
                             :show-overflow-tooltip="true" width="120">
                <template slot-scope="scope">
                    <span v-if="!scope.row.replyId || scope.row.replyId==''">未回复
                     <el-button size="mini" round @click="openReply(scope.row)">回复</el-button>
                    </span>
                    <span v-if="scope.row.replyId && scope.row.replyId!=''">已回复</span>
                </template>
            </el-table-column>

            <el-table-column sortable="custom" label="接收时间" header-align="center" align="center" prop="opAt">
                <template slot-scope="scope">
                    {{formatAt(scope.row.opAt)}}
                </template>
            </el-table-column>

        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>
    <el-dialog :close-on-click-modal="false"
            title="回复消息"
            :visible.sync="replyDialogVisible"
            width="40%">
        <el-form :model="formData" ref="dialogForm" :rules="formRules" size="small" label-width="80px">
            <el-form-item label="微信昵称">
                {{formData.nickname}}
            </el-form-item>
            <el-form-item label="消息内容">
                {{formData.msgContent}}
            </el-form-item>

            <el-form-item label="回复内容" prop="replyContent">
                <el-input type="textarea" v-trim v-model="formData.replyContent"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                    <el-button @click="replyDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="doReply">回 复</el-button>
                  </span>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            var wxConfigs = [];
            <!--#for(o in wxList){#-->
            wxConfigs.push({value: "${o.id!}", label: "${o.appname!}"});
            <!--#}#-->
            return {
                wxConfigs: wxConfigs,
                replyDialogVisible: false,
                pageForm: {
                    searchName: "",
                    searchKeyword: "",
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    pageOrderName: "opAt",
                    pageOrderBy: "descending",
                    wxid: "${wxConfig.id!}",
                },
                tableData: [],
                formData: {
                    msgid: "",
                    replyContent: "",
                    msgContent: "",
                    openid:"",
                    nickname:""
                },
                formRules: {
                    replyContent: [
                        {required: true, message: '回复内容', trigger: ['blur', 'change']}
                    ],
                },
            }
        },
        methods: {
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                $.post(base + "/platform/wx/msg/user/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code == 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            handleSelectionChange: function (val) {
                this.selectData = val;
            },
            openReply: function (obj) {
                this.replyDialogVisible = true;
                this.formData.msgid = obj.id;
                this.formData.msgContent = obj.content;
                this.formData.openid = obj.openid;
                this.formData.nickname = obj.nickname;
            },
            doReply: function () {
                var self = this;
                var url = base + "/platform/wx/msg/user/replyDo/" + self.pageForm.wxid;
                self.$refs["dialogForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.replyDialogVisible = false;
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.pageData();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            change: function (val) {
                window.location.href = "${base}/platform/wx/msg/user/" + val;
            }
        },
        created: function () {
            this.doSearch();
        }
    })
    ;
</script>
<!--#
}
#-->
